/**
 * Description:
 * Class Name:OverrideEqualsRight
 * Date:2022/1/27 13:06
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第6章_面向对象_下._6_2_处理对象;

class PersonForEqualsRight{
    private String name;
    private String idStr;
    public PersonForEqualsRight(){}
    public PersonForEqualsRight(String name, String idStr){
        this.name = name;
        this.idStr = idStr;
    }
    public String getName(){
        return this.name;
    }
    public String getIdStr(){
        return this.idStr;
    }

    public boolean equals(Object obj){
        // 如果两个对象为同一个对象
        if (this == obj)
        {
            return true;
        }
        // 只有当obj是PersonForEqualsRight对象
        if (obj != null && obj.getClass() == PersonForEqualsRight.class){
//        if (obj != null && obj instanceof PersonForEqualsRight){  // 此处不宜用instanceof，因为不严谨，目的是判断是否为同一个类的实例，instance是连子类都可以了，太广泛
            var personObj = (PersonForEqualsRight) obj;
            // 并且当前对象的idStr与obj对象的idStr相等时才可判断两个对象相等
            if (this.getIdStr().equals(personObj.getIdStr())){
                return true;
            }
        }
        return false;
    }
}

public class OverrideEqualsRight {
    public static void main(String[] args) {
        var p1 = new PersonForEqualsRight("孙悟空", "12343433433");
        var p2 = new PersonForEqualsRight("孙行者", "12343433433");
        var p3 = new PersonForEqualsRight("比克大魔王", "99999999334443");
        System.out.println(p1.equals(p2));
        System.out.println(p2.equals(p3));
    }
}
